##=============================================================================
## Appendix Figure 31
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "broom", "haven", "janitor")

ipak(packages)

##---------
# Load Data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("~/Desktop/clean_ukraine_data.RData")

##-------------------
# Survival strategies

# N.B. THESE QUESTIONS WERE NOT ASKED IN WAVE 2!

num_col <- function(x){
  any(!is.character(x))
}

surv <- raw_dat %>% 
  janitor::clean_names() %>%
  haven::zap_label() %>% 
  mutate_if(is.character, list(~na_if(., ""))) %>% 
  mutate_if(num_col, as.numeric) %>% 
  dplyr::select(v1, v149, v150_1:v150_11, v150_9txt) %>% 
  rename(id = v1,
         neutral_strat = v149,
         fled_safer_loc = v150_1,
         fled_during_fighting = v150_2,
         enlisted_urk_army = v150_3,
         ukr_defense_force = v150_4,
         support_ukr_resist = v150_5,
         inform_to_ukr_force = v150_6,
         pro_ukr_protest = v150_7,
         live_under_occupy = v150_8,
         other_strat = v150_9,
         v150_hard_to_say = v150_10,
         v150_refused = v150_11,
         survival_strat = v150_9txt) %>% 
  mutate(across((fled_safer_loc:v150_refused), ~ifelse(.x == 1, 1, 0)))

surv %>% 
  pivot_longer(., c(fled_safer_loc:other_strat), 
               names_to = "strategy") %>% 
  filter(value == 1) %>% 
  dplyr::select(strategy) %>% 
  mutate(strategy = case_when(strategy == "fled_safer_loc" ~ "Fled to safer location",
                              strategy == "fled_during_fighting" ~ "Fled during the fighting",
                              strategy == "enlisted_urk_army" ~ "Enlisted in the army",
                              strategy == "ukr_defense_force" ~ "Enlisted in a territorial \n defense unit",
                              strategy == "support_ukr_resist" ~ "Provided assistance to \n Ukrainian forces",
                              strategy == "pro_ukr_protest" ~ "Participated in a protest",
                              strategy == "inform_to_ukr_force" ~ "Informed to the armed \n forces",
                              strategy == "live_under_occupy" ~ "Lived under occupation",
                              strategy == "other_strat" ~ "Pursued another strategy")) %>% 
  count(strategy) %>% 
  ggplot(., (aes(x = reorder(strategy, n), y = n))) +
  geom_bar(stat = "identity") +
  scale_y_continuous(n.breaks = 10) +
  labs(x = NULL, 
       y = "Respondent Count") +
  hrbrthemes::theme_ipsum(axis_title_size = 24, base_size = 18) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================